GBML manual / Instances

instance_create

Creates a new instance of `obj` at (x, y) and returns a reference to it, or nothing if that object’s pool is already full.

Syntax

instance_create(x, y, obj) -> ref<object>

Arguments

NameType
xreal
yreal
objobject

Returns

ref<object>

Description

Makes a new instance of an object and hands you back a reference to it, positioned at the point you name. Everything the object's own Create event sets up (a starting speed, a picked sprite, a local script call) has already run by the time you get the reference back, so it is safe to keep configuring the new instance right away.

x and y are a place in the room, counted from the room's top-left corner: (0, 0) is that corner, x counts pixels to the right and y counts pixels down. The new instance's origin (the middle of its sprite, unless you gave it another) lands on that spot. See Where (0, 0) is.

A with block is the usual shape for that follow-up configuration: it runs its body with self switched to the instance you just made, so x/y/speed/direction and every other instance variable inside the block belong to the new instance, not the one that called instance_create.

Every object declares its own maximum instance count - read it back yourself with instance_max. A full pool is not an error - it is a normal, silent "nothing was created" outcome, which is why the result is always worth checking against nothing before you rely on it existing. Checking instance_count(obj) < instance_max(obj) before you call this at all is even better whenever the create is paired with spending something - gold, an item, a cooldown - that a silent failure would otherwise spend for nothing.

Every editor budget gauge is built from what is authored - a room's placed instances, and each object's own declared sprite. This call is the one way a project brings something alive at a moment no static pass over the project can see, so it is the one way a project can look fine in every gauge and still exceed real hardware at runtime: the new instance's sprite still claims a palette bank the first time it exists, whether or not anything was ever placed to account for it. See Budgets for exactly what this project's gauges can and cannot see, and the habits that keep runtime spawning from ever being a surprise.

Example

if (btn_pressed(BTN_A)) {
    with (instance_create(x, y, obj_doc_spark)) {
        speed = 3;
        direction = 90;
    }
}

Pressing A spawns one obj_doc_spark at the spawner's own position, then sends it moving straight down (direction = 90) at 3 pixels a frame - the with block is what lets speed/direction be written onto the spark instead of the spawner.

A real capture - nothing yet, before the spawner's own instance_create call firesA real capture - nothing yet, before the spawner's own instance_create call fires
A real capture - nothing yet, before the spawner's own instance_create call fires
The same room after the call - a new instance now exists where nothing was beforeThe same room after the call - a new instance now exists where nothing was before
The same room after the call - a new instance now exists where nothing was before

Keeping an object in a variable

A variable can hold an object itself, not just one instance of it. Write the object's name where a value goes, and later change it:

var kind = obj_zombie;
if (wave > 3) { kind = obj_brute; }

instance_create(x, y, kind);        // makes a zombie, or a brute once the wave is past 3
with (kind) { hp += 1; }            // every zombie (or every brute) gets one more hit point
var first_hp = kind.hp;             // the hit points of the first one alive, or 0 if there is none

These accept such a variable:

A calculated kind works too: instance_create(x, y, pick_enemy(wave)) and with (pick_enemy(wave)) { ... } ask the script once and use its answer.

What still cannot be written out for each kind: an instance made from a variable in the first line of a loop (while (instance_create(x, y, kind) != nothing) { ... }, because the variable could change while the loop runs; the same call as a number, like while (instance_count(kind) < 5), is fine), and a value given to kind.hp in the middle of another expression (v = (kind.hp = 3); put it on a line of its own). A kind cannot be handed to a script as a parameter either.

Gbabel works out for itself which objects your game ever puts in such variables (every object name you assign, compare, hand to a script or return from one) and writes your lines out once for each of them, exactly as you would have typed them with an if per kind. The more different objects you keep in variables, the more code that is, so a game that keeps two kinds in a variable pays for two. When the answer of instance_create is kept (var made = ...), the lines after it in the same block are written out for each kind too, so keep that part short (put a long stretch of code in a script that takes the new instance). A game that never does this is built exactly as before.

On the GBA

Every object declares its own maximum instance count (readable yourself as instance_max(obj)); a full pool is a normal, silent "nothing created" outcome here, never a crash - always check the result against `nothing` before using it, exactly like the with (instance_create(...)) { ... } form. Checking instance_count(obj) < instance_max(obj) BEFORE calling this avoids the wasted attempt entirely - the right habit whenever a create is paired with spending something (gold, a cooldown, an item) that a silent failure would otherwise spend for nothing.

See also: instance_destroyinstance_existsinstance_countinstance_maxObjects and instancesBudgets